package fr.utbm.aiechat.structures;

import java.io.Serializable;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import fr.utbm.aiechat.R;


public class SharedFileAdapter extends ArrayAdapter<SharedFile> implements Serializable
{
	private static final long serialVersionUID = -2371930434803830486L;
	private Context context;
	private int textViewResourceId;
	
	public SharedFileAdapter(Context context, int textViewResourceId)
	{
		super(context, textViewResourceId);
		this.context = context;
		this.textViewResourceId = textViewResourceId;
	}

	@Override
    public View getView(int position, View convertView, ViewGroup parent)
	{
        View row = convertView;
        FileHolder holder = null;
       
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(textViewResourceId, parent, false);
           
            holder = new FileHolder();
            holder.Filename = (TextView)row.findViewById(R.id.sharedfile);
           
            row.setTag(holder);
        }
        else
        {
            holder = (FileHolder)row.getTag();
        }
       
        SharedFile file = getItem(position);
        
        if(file.getSize() < 1024)
        {
        	holder.Filename.setText(file.getName() + " (" + (String.valueOf(file.getSize()) ) + " Bytes)" );
        }
        else
        {
        	holder.Filename.setText(file.getName() + " (" + (String.valueOf(file.getSize() / 1024) ) + " Kb)" );
        }
        
       
        return row;
    }
   
    static class FileHolder
    {
        TextView Filename;
    }
}

